var app = getApp()

Page({
  data: {
    oldPwd: '',
    newPwd: '',
    lastPwd: '',
    password: ''
  },
  onLoad: function (options) {
    var targetstr
    if (options.target === 'check') {
      targetstr = 'check'
    }
    else {
      targetstr = 'set'
      if (app.globalData.isPwd === true) {
        targetstr = 'update'
      }
    }
    if (targetstr === 'set') {
      wx.setNavigationBarTitle({
        title: '设置余额使用密码',
      })
    }
    else if (targetstr === 'update') {
      wx.setNavigationBarTitle({
        title: '修改余额使用密码',
      })
    }
    else if (targetstr === 'check') {
      wx.setNavigationBarTitle({
        title: '余额使用密码',
      })
    }
    this.setData({
      target: targetstr
    })
  },
  forgetPwd: function () {
    var phone = app.globalData.personInfo.MOBILE
    var msg = '发送（临时密码）到您的激活手机号码：' + phone
    var that = this
    wx.showModal({
      title: '',
      content: msg,
      confirmColor: '#a08257',
      confirmText: '获取密码',
      cancelText: '关闭',
      success: function (res) {
        if (res.confirm) {
          that.sendMsgAction()
        }
      }
    })
  },
  sendMsgAction: function () {
    var phone = app.globalData.personInfo.MOBILE
    var userid = app.globalData.userid
    var url = '/yuanmai_we/user/password/forgot'
    var property = {
      "USERID": userid,
      "MOBILE": phone
    }
    var params = {
      __zaofans: true,
      datas: JSON.stringify(property)
    }
    app.request(url, params).then(res => {
      if (res.success) {
        app.toast.success('发送成功')
      }
    })
  },
  goBack: function () {
    wx.navigateBack()
  },
  oldPwdInput: function (e) {
    this.setData({
      oldPwd: e.detail.value
    })
  },
  newPwdInput: function (e) {
    this.setData({
      newPwd: e.detail.value
    })
  },
  lastPwdInput: function (e) {
    this.setData({
      lastPwd: e.detail.value
    })
  },
  usePwdInput: function (e) {
    this.setData({
      password: e.detail.value
    })
  },
  submitPwd: function () {
    if (this.data.target === 'check') {
      if (this.data.password === '') {
        app.toast.error('请输入密码')
        return
      }
      var url = '/yuanmai_we/user/password/verify'
      var userid = app.globalData.userid
      var property = {
        "USERID": userid,
        "PASSWORD": this.data.password
      }
      var params = {
        __zaofans: true,
        datas: JSON.stringify(property)
      }
      app.request(url, params).then(this.confirmFreePayCallBack.bind(this))
    }
    else {
      if (this.data.target === 'update') {
        if (this.data.oldPwd === '') {
          return app.toast.error('请输入旧密码')
        }
      }

      if (this.data.newPwd === '') {
        return app.toast.error('请输入新密码')
      }

      if (this.data.newPwd.length > 8 || this.data.newPwd.length < 6) {
        return app.toast.error('密码限制6~8位')
      }

      if(/[^\x00-\xff]/.test(this.data.newPwd)){
        return app.toast.error('密码限制数字、字母或英文字符')
      }

      if (this.data.lastPwd === '') {
        return app.toast.error('请输入确认密码')
      }

      if (this.data.newPwd !== this.data.lastPwd) {
        return app.toast.error('密码不一致')
      }

      var url = '/yuanmai_we/user/userinfo/updatepwd'
      var property = {
        "oldPwd": null,
        "newPwd": this.data.lastPwd
      }

      if (this.data.target === 'update') {
        property.oldPwd = this.data.oldPwd
      }

      var params = {
        datas: JSON.stringify(property),
        __zaofans: true
      }

      app.request(url, params).then(this.submitPwdCallBack.bind(this))
    }
  },
  submitPwdCallBack: function (res) {
    if (res.result === 'success') {
      app.toast.success('设置成功')
      setTimeout(() => {
        wx.navigateBack()
      }, 1000)
    }
    else {
      app.toast.error(res.msg)
    }
  },
  confirmFreePayCallBack: function (res) {
    if (res.success) {
      app.toast.success('使用成功')
      setTimeout(() => {
        app.globalData.tempUseBalance = '1'
        wx.navigateBack()
      }, 1000)
    }
    else {
      app.toast.error(res.errmsg)
    }
  }
})
